!<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title></title>
	<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
	<script type="text/javascript" src="http://apps.bdimg.com/libs/layer/2.1/layer.js"></script>
	<!-- https://blog.csdn.net/yondao/article/details/134529022 -->
	<!-- https://www.taotaoit.com/article/details/1468.html -->
</head>
<body>
<!-- 
<?php echo Html::a('签到自动开票开关', 'javascript:;', ['class' => 'btn btn-warning lpaction', 'data-action' => 'zidongkaipiao',
        'data-select' => '1-开启|0-关闭', 'data-value' => \common\components\MeetHelper::qiandaoAutoKaipiaoStatus()]); ?>

    <?php echo Html::a('批量下载电子发票', 'javascript:;', ['class' => 'btn btn-warning lpaction', 'data-action' => 'downfapiao']); ?>
    <?php echo Html::a('批量更新票号', 'javascript:;', ['class' => 'btn btn-warning lpaction', 'data-action' => 'bulk-update-bianhao-invoice']); ?> -->

<a class="btn btn-warning lpaction" href="javascript:;" data-action="zidongkaipiao" data-select="1-开启|0-关闭" data-value="0">签到自动开票开关</a>
<a class="btn btn-warning lpaction" href="javascript:;" data-action="downfapiao">批量下载电子发票</a>
<a class="btn btn-warning lpaction" href="javascript:;" data-action="bulk-update-bianhao-invoice">批量更新票号</a>


<script type="text/javascript">
	const lptips_select = '请选择操作项目';
	function lpaction() {
            $('.lpaction').click(function () {
                var _that = $(this);
                if ($(this).data('action') == 'zidongkaipiao') {
                    // 监听变化状态接口
                    var value = $(this).data('value');
                    var select = $(this).data('select');
                    var selectArr = select.split('|');
                    var selectOption = [];
                    var html = '';
                    html += '<div style="margin: 10px;"><label>自动开票状态</label><select class="lpvalue form-control">'
                    $.each(selectArr, function (i, v) {
                        selectOption = v.split('-');
                        if (value == selectOption[0]) {
                            html+= '<option value="' + selectOption[0] +  '" selected>' + selectOption[1] + '</option>';
                        } else {
                            html+= '<option value="' + selectOption[0] +  '">' + selectOption[1] + '</option>';
                        }
                    });
                    html+= '</select></div>';

                    layer.open({
                        title: '提示',
                        type: 1,
                        shadeClose: true,
                        shade: 0.8,
                        area: ['400px', '300px'],
                        closeBtn: false,
                        content: html,
                        btn: ['保存'],
                        yes: function(index){
                            var kaipiaoStatus = $('.lpvalue').val();
                            $.ajax({
                                url: '/invoice/save-qiandao-auto-kaipiao',
                                type: 'POST',
                                dataType: 'json',
                                data: {
                                    'kaipiaostatus': kaipiaoStatus
                                },
                                success:function (data) {
                                    if (data.code == 0) {
                                        _that.data('value', kaipiaoStatus);
                                        layer.closeAll();
                                        layer.msg(data.msg);
                                    } else {
                                        layer.msg(data.msg);
                                    }
                                }
                            })
                        }
                    });
                } else if ($(this).data('action') == 'bulk-kaidpiao') {
                    bulkKaidpiao();
                } else if ($(this).data('action') == 'downfapiao') {
                    downfapiao();
                } else if ($(this).data('action') == 'bulk-update-bianhao-invoice') {
                    updatebianhao();
                }
            });
        }

        function bulkKaidpiao() {
            var pks = [];
            console.log(pks);
            $(".kv-row-checkbox").each(function (index, el) {
                if ($(this).is(":checked")) {
                    pks.push($(this).val());
                }
            });
            if (pks.length == 0) {
                layer.msg(lptips_select);
                return false;
            }

            var pksStr = pks.join(',');

            layer.open({
                title: '提示',
                type: 1,
                shadeClose: true,
                shade: 0.8,
                area: ['400px', '300px'],
                closeBtn: false,
                content: '<div style="padding: 10px;">是否要批量开票</div>',
                btn: ['确认'],
                yes: function (index) {
                    pageAjaxTongyong('/invoice/bulk-kaidpiao-new', 'POST', pksStr, 1, 1);
                }
            })
        }

        function pageAjaxTongyong(url, action, pksStr, pageSize, page) {
            $.ajax({
                url: url,
                type: action,
                dataType: 'json',
                data: {ids: pksStr, pageSize: pageSize, page: page, token: ajaxtoken},
            })
                .done(function (data) {
                    if(data.code == 0) {
                        if (data.finish == 1) {
                            layer.closeAll();
                            if (data.downloadUrl) {
                                layer.open({
                                    type: 1
                                    , title: '<?php echo Yii::t("app", "tips"); ?>'
                                    // , area: ['300px', '160px']
                                    , content: '<div style="padding: 20px;"><a href = "'+data.downloadUrl+'">打包完成，请点击<span style="text-decoration:underline;color:red">进行下载</span></a><div>' + data.msg + '</div></div>'
                                    , btn: '<?php echo Yii::t("app", "关闭"); ?>'
                                    , btnAlign: 'c'
                                    , yes: function (index) {
                                        layer.closeAll();
                                    }
                                })
                            } else {
                                layer.alert(data.msg);
                            }

                        } else {
                            layer.closeAll();
                            layer.alert(data.msg);
                            pageAjaxTongyong(url, action, pksStr, pageSize, data.page);
                        }
                    } else {
                        layer.closeAll();
                        layer.alert(data.msg);
                    }
                })
        }

        var ajaxtoken = '';
        function downfapiao() {
            ajaxtoken = Date.now();

            var pks = [];
            console.log(pks);
            $(".kv-row-checkbox").each(function (index, el) {
                if ($(this).is(":checked")) {
                    pks.push($(this).val());
                }
            });
            if (pks.length == 0) {
                layer.msg(lptips_select);
                return false;
            }

            var pksStr = pks.join(',');

            layer.open({
                title: '提示',
                type: 1,
                shadeClose: true,
                shade: 0.8,
                area: ['400px', '200px'],
                closeBtn: false,
                content: '<div style="padding: 10px;">是否要下载电子发票</div>',
                btn: ['确认'],
                yes: function (index) {
                    layer.load(2);
                    pageAjaxTongyong('/invoice/downfapiao', 'POST', pksStr, 20, 1);
                }
            })
        }

        function updatebianhao() {
            ajaxtoken = Date.now();

            var pks = [];
            console.log(pks);
            $(".kv-row-checkbox").each(function (index, el) {
                if ($(this).is(":checked")) {
                    pks.push($(this).val());
                }
            });
            if (pks.length == 0) {
                layer.msg(lptips_select);
                return false;
            }

            var pksStr = pks.join(',');

            layer.open({
                title: '提示',
                type: 1,
                shadeClose: true,
                shade: 0.8,
                area: ['400px', '200px'],
                closeBtn: false,
                content: '<div style="padding: 10px;">是否要批量更新票号</div>',
                btn: ['确认'],
                yes: function (index) {
                    layer.load(2);
                    pageAjaxTongyong('/invoice/bulk-update-bianhao-invoice', 'POST', pksStr, 20, 1);
                }
            })
        }

        lpaction();
        $(document).on('pjax:success', function() {
            lpaction();
        })
</script>
</body>
</html>